import 'dart:io';

import 'package:flutter_syntax_view/flutter_syntax_view.dart';
import 'package:path_provider/path_provider.dart';

class FileUtil {

  static const String IMAGE_SUFFIXS = ",bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,"
      "webp,avif,apng,";

  // excel后缀
  static const String EXCEL_SUFFIXS = ",xls,xlsx,csv,";
  // zip后缀
  static const String ZIP_SUFFIXS = ",zip,rar,7z,tar,gz,";
  // doc后缀
  static const String DOC_SUFFIXS = ",doc,docx,";
  static const String PDF_SUFFIXS = ",pdf,";

  static const String XML_SUFFIXS = ",xml,";

  static const String TXT_SUFFIXS = ",txt,";
  static const String TXT_EXT_SUFFIXS = ",sql,ini,properties,xml,";
  static const String HTML_SUFFIXS = ",html,htm,";
  static const String MD_SUFFIXS = ",md,";

  // audio后缀
  static const String AUDIO_SUFFIXS = ",mp3,wma,aac,ogg,wav,flac,ape,midi,cda,au,aiff,mp1,mp2,";
  static const String VEDIO_SUFFIXS = ",mp4,";
  // ppt后缀
  static const String PPT_SUFFIXS = ",ppt,pptx,";
  // exe后缀
  static const String EXE_SUFFIXS = ",exe,bat,";

  static bool fileExisted(String filePath) {
    var file = File(filePath);
    return file.existsSync();
  }

  // 如果文件不存在，就返回錯誤文件圖標
  static String fileNotExistedAndErrorPath(String filePath) {
    if (!fileExisted(filePath)) {
      return "assets/svg/file_error.svg";
    }
    return filePath;
  }

  static Future<void> moveFile(String sourcePath, String destPath) async {
    var file = File(sourcePath);
    await file.rename(destPath);
  }

  static Future<String> getFileRoot() async {
    if (Platform.isWindows) {
      String dir = Directory.current.path;
      return dir;
    } else if (Platform.isAndroid || Platform.isIOS) {
      Directory directory = await getApplicationDocumentsDirectory();
      return directory.path;
    }
    return "";
  }

  static void mkDirs(String filePath) {
    final folderDirectory = Directory(filePath);
    folderDirectory.createSync(recursive: true);
  }

  static String getFileImagePath(String suffix) {
    if (suffix == '') {
      return "assets/img/file/file.png";
    }
    if (suffix.contains(".")) {
      suffix = getSuffix(suffix);
    }

    if (DOC_SUFFIXS.contains(suffix)) {
      return "assets/img/file/DOC.png";
    } else if (isPdf(suffix)) {
      return "assets/img/file/PDF.png";
    } else if (EXCEL_SUFFIXS.contains(suffix)) {
      return "assets/img/file/EXCEL.png";
    } else if (ZIP_SUFFIXS.contains(suffix)) {
      return "assets/img/file/zip.png";
    } else if (XML_SUFFIXS.contains(suffix)) {
      return "assets/img/file/xml.png";
    } else if (HTML_SUFFIXS.contains(suffix)) {
      return "assets/img/file/HTML.png";
    } else if (TXT_SUFFIXS.contains(suffix)) {
      return "assets/img/file/txt.png";
    } else if (MD_SUFFIXS.contains(suffix)) {
      return "assets/img/file/MD.png";
    } else if (PPT_SUFFIXS.contains(suffix)) {
      return "assets/img/file/PPT.png";
    } else if (VEDIO_SUFFIXS.contains(suffix)) {
      return "assets/img/file/vedio.png";
    } else {
      return "assets/img/file/file.png";
    }
  }

  static String getSuffix(String fileName) {
    var suffix = fileName;
    var index = fileName.lastIndexOf(".");
    if (index > 0) {
      suffix = fileName.substring(index+1);
      suffix = suffix.toLowerCase();
    }
    return suffix;
  }

  static String formatFileSize(int fileSize) {
    if (fileSize < 0) return "";
      double fsize = fileSize * 1.0;
      var suffixs = ["B", "K", "M", "G", "T", "P"];
      for(int i=0, lg=suffixs.length; i<lg; i++) {
        if (fsize <= (i+1) * 1024) {
          return fsize.toStringAsFixed(2) + suffixs[i];
        }
        fsize = fsize/1024;
    }
    return "";
  }

  static bool canShow(String fileName) {
    return isPdf(fileName) || isVideo(fileName) || isDocument(fileName) || isCode(fileName) || isText(fileName);
  }

  static bool isImage(String fileName) {
    if (fileName == null || fileName == '') return false;
    var suffix = FileUtil.getSuffix(fileName);
    return IMAGE_SUFFIXS.contains(suffix);
  }

  static bool isPdf(String fileName) {
    String suffix = getSuffix(fileName);
    return PDF_SUFFIXS.contains(suffix);
  }

  static bool isVideo(String fileName) {
    String suffix = getSuffix(fileName);
    bool isVideo = VEDIO_SUFFIXS.contains(suffix);
    return isVideo;
  }

  static bool isDocument(String fileName) {
    String suffix = getSuffix(fileName);
    return DOC_SUFFIXS.contains(suffix) || EXCEL_SUFFIXS.contains(suffix) || PPT_SUFFIXS.contains(suffix);
  }

  static bool isCode(String fileName) {
    return getCodeSyntax(fileName) != null;
  }

  static bool isText(String fileName) {
    String suffix = getSuffix(fileName);
    return TXT_SUFFIXS.contains(suffix) || TXT_EXT_SUFFIXS.contains(suffix);
  }

  static Syntax? getCodeSyntax(String fileName) {
    String suffix = getSuffix(fileName);
    Syntax syntax;
    if (suffix == 'java') {
      syntax = Syntax.JAVA;
    } else if (suffix == 'h') {
      syntax = Syntax.C;
    } else if (suffix == 'dart') {
      syntax = Syntax.DART;
    } else if (suffix == 'js') {
      syntax = Syntax.JAVASCRIPT;
    } else if (suffix == 'cpp') {
      syntax = Syntax.CPP;
    } else if (suffix == 'yml') {
      syntax = Syntax.YAML;
    } else {
      return null;
    }
    return syntax;
  }
}